//
//  NYCourseViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/10.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYCourseViewController.h"
#import "NYDetailContentViewController.h"
#import "NYExamineViewStartTestViewController.h"
#import "NYExamineTestViewController.h"

//request
#import "NYCourseRequest.h"

//models
#import "NYCourseModel.h"

//views
#import "NYCourseHeaderSectionView.h"


@interface NYCourseViewController ()

@property (nonatomic, assign) NYExamineRequestStatus status;

@property (nonatomic, strong) NYCourseRequest *request;

@property (nonatomic, copy) NSString *idString;

@property (nonatomic, strong) NYCourseModel *detailModel;

@property (nonatomic, strong) NSMutableDictionary *paraDictionary;
@end

@implementation NYCourseViewController

#pragma mark - Life Cycle Methods
- (instancetype)initWithDetailContentStatus:(NYExamineRequestStatus)status withId:(NSString *)idString {
    self = [super init];
    if (self) {
        _status = status;
        _idString = idString;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
#pragma mark - Intial Methods
- (void)initUI {
    
    [self loadData];
}
- (void)initTableView {
    [super initTableView];

}
- (void)configNimbus {
    [super configNimbus];

    [self.tableView registerClass:[NYCourseHeaderSectionView class] forHeaderFooterViewReuseIdentifier:[NYCourseHeaderSectionView className]];

    @weakify(self);
    [self.tableViewActions attachToClass:[NYVideoModel class] tapBlock:^BOOL(id object, id target, NSIndexPath *indexPath) {

        @strongify(self);
        if (indexPath.section == 1) {
            NYVideoModel *videoModel = self.detailModel.video[indexPath.row];
            [self.paraDictionary setObject:@(videoModel.id).stringValue forKey:kRequestVideoID];
            NYDetailContentViewController *ViewController = [[NYDetailContentViewController alloc] initWithParaDictionary:self.paraDictionary withDeatilModel:self.detailModel preparePlayModel:videoModel preparePlayModelIndex:indexPath.row examineStatus:self.status];
            [self.navigationController pushViewController:ViewController animated:true];
        }
        return true;
    }];
    [self.tableViewActions attachToClass:[NYExaminationModel class] tapBlock:^BOOL(id object, id target, NSIndexPath *indexPath) {

        @strongify(self);
        if (indexPath.section == 2) {

            NYExaminationModel *examinationModel = self.detailModel.examination[indexPath.row];

            if (self.status == NYExamineRequestStatusCup) { //考试首页

                NYExamineViewStartTestViewController *ViewController = [[NYExamineViewStartTestViewController alloc] initWithExamineRequestStatus:self.status withExamineID:@(examinationModel.id).stringValue];
                [self.navigationController pushViewController:ViewController animated:true];
            } else if (self.status == NYExamineRequestStatusChoose) { //考试详情页

                NYExamineTestViewController *vc = [[NYExamineTestViewController alloc] initWithExamineRequestStatus:self.status withExamineID:@(examinationModel.id).stringValue];
                [self.navigationController pushViewController:vc animated:true];

            }
        }
        return true;
    }];

}
#pragma mark - Network Methods
- (void)loadData {

    [self configNimbus];

    [self setLoading:YES animation:YES];
    [self.tableViewModel removeAll];
    [self.request clearCache];

    NSDictionary *paraDict = @{};
    if (_status == NYExamineRequestStatusChoose) {

        paraDict = @{kRequestKeyChannel:@(NYUserManager.channelID), kRequestKeyLoginCode : NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.logincode), kRequestKeyUser_ID:NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.user_id), kRequestKeyCourseID :NOT_NULL_STRING_DEFAULT_EMPTY(self.idString)};
    } else if (_status == NYExamineRequestStatusCup) { //杯赛
        paraDict = @{kRequestKeyChannel:@(NYUserManager.channelID), kRequestKeyLoginCode : NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.logincode), kRequestKeyUser_ID:NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.user_id), kRequestKeyCupID :NOT_NULL_STRING_DEFAULT_EMPTY(self.idString)};
    }

    [self.paraDictionary addEntriesFromDictionary:paraDict];

    self.request = [NYCourseRequest requestWithType:_status param:self.paraDictionary];

    @weakify(self);
    [self.request startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYCourseModel * _Nonnull data) {
        @strongify(self);
        self.detailModel = data;
        self.detailModel.isRecord = false;
        [self setSuccess];
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        @strongify(self);
        [self setError:error];
    }];

}

#pragma mark - Target Methods

#pragma mark - Public Methods

#pragma mark - Private Method
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar {
    [super configNavigationBar:navigationBar];

//    self.title = @"";
}
+ (NYViewControllerNavigationBarType)navigationBarType {

    return NYViewControllerNavigationBarTypeBlue;
}
- (BOOL)showChoseChannelIDRightView {
    return false;
}

#pragma mark - External Delegate

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return __WGHeight(10);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 0.f : 45.f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[UITableViewHeaderFooterView className]];
    footer.contentView.backgroundColor = UIColorForBackground;
    return footer;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NYCourseHeaderSectionView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[NYCourseHeaderSectionView className]];
    if (section == 1) {
        [header setTitle:@"课程目录"];
    } else if (section == 2){
        [header setTitle:@"测试卷"];
    }
    return header;
}
#pragma mark - Setter Getter Methods
- (void)setDetailModel:(NYCourseModel *)detailModel {
    _detailModel = detailModel;
    self.title = detailModel.title;
    [self.tableViewModel addObject:detailModel];
    [self.tableViewModel addSectionWithTitle:nil];
    [self.tableViewModel addObjectsFromArray:detailModel.video];
    [self.tableViewModel addSectionWithTitle:nil];
    [self.tableViewModel addObjectsFromArray:detailModel.examination];

    [self.tableView reloadData];
}
- (NSMutableDictionary *)paraDictionary {
    if (!_paraDictionary) {
        _paraDictionary = [NSMutableDictionary dictionary];
    }
    return _paraDictionary;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
